package com.example.shellshopping.goods;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shellshopping.R;
import com.example.shellshopping.personCenter.PersonSettingRepwdActivity;
import com.example.shellshopping.utils.BaseFragment;
import com.example.shellshopping.utils.URLUtils;
import com.google.gson.Gson;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class GoodsTypeFragment extends BaseFragment {


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    static String tv1Str;
    static String tv2Str;
    static String tv3Str;
    static String ivStr;
    ListView lv;
    List<GoodsBean.DataBean> data;
    GoodsDetailsBean.DataBean dataDetailBean;
    private GoodsTypeAdapter adapter;

    public GoodsTypeFragment() {
        // Required empty public constructor
    }

    public static GoodsTypeFragment newInstance(String param1, String param2) {
        GoodsTypeFragment fragment = new GoodsTypeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_goods_type,container,false);
        lv = view.findViewById(R.id.frag_goodsType_lv);
        //数据源
        data = new ArrayList<>();
        //适配器
        adapter = new GoodsTypeAdapter(getContext(),data);
        lv.setAdapter(adapter);
        //联网
        getNetwork(URLUtils.queryCommodityByCateId_url+mParam1);
        setListener();
        return view;
    }

    private void setListener() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳转商品详情界面
//                Test test = new Test();
//                test.jumpToDetails(position);
//                jumpToDetails();
                //jumpToDetails(position);

                GoodsBean.DataBean dataBean = data.get(position);
                String str = URLUtils.queryCommodityInfo_url+dataBean.getId();
                RequestParams params = new RequestParams(str);
                x.http().get(params, new CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        GoodsDetailsBean goodsBean = new Gson().fromJson(result,GoodsDetailsBean.class);//解析数据
                        dataDetailBean = goodsBean.getData();
                        Intent intent = new Intent(getContext(), GoodsDetailsActivity.class);
                        intent.putExtra("goods",dataDetailBean);  //（替换方法）将数据传到下一个界面，保证key （"goods"）相同
                        startActivity(intent);
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }
        });
    }


    @Override
    public void onSuccess(String result){
        super.onSuccess(result);
        //Toast.makeText( getContext(), "查询成功", Toast.LENGTH_SHORT ).show();
        GoodsBean goodsBean = new Gson().fromJson(result,GoodsBean.class);//解析数据
        List<GoodsBean.DataBean> list = goodsBean.getData();//获取网络集合
        data.addAll(list);//将网络数据添加到list view的数据源当中
        adapter.notifyDataSetChanged();//提示更新
    }

    /*public class Test extends BaseFragment{
        Test (){
        }
        void jumpToDetails(int position) {

            GoodsBean.DataBean dataBean = data.get(position);
            getNetwork(URLUtils.queryCommodityInfo_url+dataBean.getId());

        }

        @Override
        public void onSuccess(String result){
            super.onSuccess(result);
            GoodsDetailsBean goodsBean = new Gson().fromJson(result,GoodsDetailsBean.class);//解析数据
            dataDetailBean = goodsBean.getData();
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            super.onError(ex, isOnCallback);
        }

        @Override
        public void onFinished() {
            super.onFinished();
        }

    }*/

}